package com.lzxuni.modules.plantingprogram.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzxuni.common.utils.R;
import com.lzxuni.common.utils.StringUtils;
import com.lzxuni.common.utils.UuidUtil;
import com.lzxuni.modules.common.controller.BaseController;
import com.lzxuni.modules.common.entity.PageData;
import com.lzxuni.modules.common.entity.PageParameter;
import com.lzxuni.modules.plantingprogram.entity.PlantingProgram;
import com.lzxuni.modules.plantingprogram.entity.PlantingProgramDetail;
import com.lzxuni.modules.plantingprogram.entity.PlantingProgramUse;
import com.lzxuni.modules.plantingprogram.service.PlantingProgramDetailService;
import com.lzxuni.modules.plantingprogram.service.PlantingProgramService;
import com.lzxuni.modules.plantingprogram.service.PlantingProgramUseService;
import com.lzxuni.modules.weather.entity.WeatherLimitDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * <p>
 * 种植方案_信息 前端控制器
 * </p>
 *
 * @author LaoNa
 * @since 2020-12-18
 */
@RestController
@RequestMapping("/PlantingProgram")
public class PlantingProgramController extends BaseController {


    //种植方案
    @Autowired
    private PlantingProgramService plantingProgramService;

    //种植方案明细
    @Autowired
    private PlantingProgramDetailService plantingProgramDetailService;
    //种植方案使用
    @Autowired
    private  PlantingProgramUseService plantingProgramUseService;



    //主页面
    @RequestMapping("/Index")
    public ModelAndView Index() {
        ModelAndView mv = new ModelAndView("/PlantingProgram/index");
        return mv;
    }

    //查询数据
    @RequestMapping("/GetList")
    public Object GetList(String pagination,PlantingProgram plantingProgram ){
        PageParameter pageParameter = JSON.parseObject(pagination, PageParameter.class);
//        PlantingProgram plantingProgram = JSON.parseObject(queryJson, PlantingProgram.class);
        PageData pageData = getPageData(plantingProgramService.queryPage(pageParameter, plantingProgram));
        return R.ok().put("data", pageData);
    }



    //表单页
    @RequestMapping("/Form")
    public ModelAndView Form(String type,String id) {
        ModelAndView mv = new ModelAndView("/PlantingProgram/form");
        if(StringUtils.isNotBlank(type)){
            if(StringUtils.isNotBlank(id)){
                List<PlantingProgramDetail> list=plantingProgramDetailService.list(new QueryWrapper<PlantingProgramDetail>().eq("program_id",id).orderByAsc("create_date"));
                if(type.equals("edit")||type.equals("query")){
                    mv.addObject("detailList",list);
                    mv.addObject("detailCoount",list.size());

                }
            }
            mv.addObject("type",type);
        }
        return mv;
    }


    //删除数据
    @RequestMapping("/DeleteForm")
    public Object delete(String keyValue) {
        List<PlantingProgramUse> plantingProgramUseList= plantingProgramUseService.list(new QueryWrapper<PlantingProgramUse>().eq("program_id",keyValue));
        if(plantingProgramUseList!=null&&plantingProgramUseList.size()>0){
            return R.error(440,"已被使用禁止删除");
        }else {
            plantingProgramService.removeById(keyValue);
            plantingProgramDetailService.remove(new QueryWrapper<PlantingProgramDetail>().eq("program_id", keyValue));
            return R.ok("删除成功");
        }
    }


    @RequestMapping("/IsOpen")
    public Object isOpen(String keyValue,String type) {
        if(StringUtils.isNotBlank(keyValue)&&StringUtils.isNotBlank(type)){
            plantingProgramService.updateById(new PlantingProgram().setProgramId(keyValue).setIsOpen(type));
            if(type.equals("1"))
                return R.ok("启用成功");
            if(type.equals("0"))
                return R.ok("关闭成功");
        }
        return R.error("异常");
    }
    @RequestMapping("/copy")
    public Object isOpen() {
        plantingProgramService.copy();
        return R.ok("复制成功");
    }

    //保存数据
    @RequestMapping("/SaveForm")
    public Object insertDo(String keyValue, PlantingProgram plantingProgram, String subJson) {
        plantingProgram.setProgramId(keyValue);
        plantingProgram.setCropId(plantingProgram.getCropModel());
        List<PlantingProgramDetail> plantingProgramDetailList = JSON.parseArray(subJson, PlantingProgramDetail.class);
        String id = "";
        if (StringUtils.isEmpty(plantingProgram.getProgramId())) {
            id = UuidUtil.get32UUID();
            plantingProgram.setProgramId(id);
        } else {
            id = plantingProgram.getProgramId();
        }
        if (plantingProgramDetailList != null && plantingProgramDetailList.size() > 0) {
            for (PlantingProgramDetail plantingProgramDetail : plantingProgramDetailList) {
                plantingProgramDetail.setProgramId(id);
            }
        }
        if (StringUtils.isEmpty(keyValue)) {
            plantingProgramService.save(plantingProgram);
            plantingProgramDetailService.saveBatch(plantingProgramDetailList);
            return R.ok("保存成功");
        } else {
            plantingProgramDetailService.remove(new QueryWrapper<PlantingProgramDetail>().eq("program_id",id));
            plantingProgramService.updateById(plantingProgram);
            plantingProgramDetailService.saveBatch(plantingProgramDetailList);
            return R.ok("修改成功");
        }
    }

}

